<?php

declare (strict_types=1);
namespace App\Model;

use Hyperf\Database\Model\Relations\BelongsTo;
use Hyperf\Database\Model\Relations\BelongsToMany;
use Hyperf\Database\Model\Relations\HasOne;
use Hyperf\Database\Model\SoftDeletes;

/**
 * @property int $id 
 * @property int $parent_id 
 * @property int $group_id 
 * @property string $phone 
 * @property string $login_pwd 
 * @property string $safe_pwd 
 * @property string $avatar 
 * @property string $name 
 * @property int $sex 
 * @property string $sign 
 * @property string $email 
 * @property float $money
 * @property float $cumulatived_money
 * @property int $integral
 * @property int $birthday 
 * @property int $status 
 * @property string $identity_code 
 * @property \Carbon\Carbon $created_at 
 * @property \Carbon\Carbon $updated_at 
 * @property int $deleted_at
 * @property UserGroup $group
 * @property Business $business
 */
class User extends Model
{
    use SoftDeletes;

    /**
     * 时间戳格式化方式
     *
     * @var string
     */
    public $dateFormat = 'U';

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'user';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $guarded = [];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = ['id' => 'int', 'parent_id' => 'integer', 'group_id' => 'integer', 'sex' => 'integer', 'money' => 'float', 'integral' => 'integer', 'birthday' => 'date:Y-m-d', 'status' => 'boolean', 'created_at' => 'datetime', 'updated_at' => 'datetime', 'deleted_at' => 'integer'];

    /**
     * 用户组关联模型
     *
     * @return BelongsTo
     */
    public function group(): BelongsTo
    {
        return $this->belongsTo(UserGroup::class, 'group_id');
    }

    /**
     * 用户商家关联
     *
     * @return mixed
     */
    public function business(): hasOne
    {
        return $this->hasOne(Business::class, 'user_id');
    }

    /**
     * 用户商品收藏关联
     *
     * @return BelongsToMany
     */
    public function favoriteGoods() :BelongsToMany
    {
        return $this->belongsToMany(Goods::class, 'user_fav')->orderBy('user_fav.fav_time', 'desc');
    }

    /**
     * 用户和积分关联
     *
     * @return \Hyperf\Database\Model\Relations\HasMany
     */
    public function userIntegral()
    {
        return $this->hasMany(IntegralDetail::class, 'user_id', 'id');
    }
}